package com.situ.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.Product;
import com.service.IProductService;
import com.situ.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductService implements IProductService {
@Autowired
    ProductMapper productMapper;

    @Override
    public Product inseret(Product product) {

         productMapper.insert(product);

       return   productMapper.selectById(product.getId());


    }

    @Override
    public Product update(Product product) {
      productMapper.update(product,null);
      return productMapper.selectById(product.getId());

    }

    @Override
    public Product getById(Integer id) {
      return  productMapper.selectById(id);
    }

    @Override
    public int deleteById(Integer id) {
        return productMapper.deleteById(id);
    }

      @Override
    public List<Product> getListByPage(IPage page, Product product) {
        Wrapper wrapper=new QueryWrapper(product);
      List<Product> list = productMapper.selectList(page,wrapper);
        return list;
    }

    @Override
    public List<Product> getList(Product product) {
        Wrapper wrapper=new QueryWrapper(product);
        List<Product> list = productMapper.selectList(wrapper);
        return list;
    }

    @Override
    public int setActive(Product product) {
      UpdateWrapper wrapper = new UpdateWrapper();
       if(product.getIs_active()==1){
           wrapper.set("is_active",0);

       }else{
           wrapper.set("is_active",1);
       }

         wrapper.eq("id",product.getId());
         int result = productMapper.update( wrapper);
         return result;
    }
}
